/**
 * Copyright(C) 2019 FUYUN DATA SERVICES CO.,LTD. - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * 该源代码版权归属福韵数据服务有限公司所有
 * 未经授权，任何人不得复制、泄露、转载、使用，否则将视为侵权
 */

import { createModel } from "@rematch/core";
import { IRootState } from "../../store";
import { message } from "antd";
import {
  getGoodsList,
  saveGoods,
  getGoodsById,
  editGoods,
  uploadGoods,
  deleteGoods,
  findByProId
} from "src/pages/Goods/components/goodsManage/GoodsAPI";
import { IProduct } from "../product/Product";

export interface IGoods {
  id: string;
  name: string;
  proId: string;
  product: string;
  productBatch: string;
  spec: string;
  productionTime: string;
}

interface IState {
  goodsList: IGoods[];
  goodsById: IGoods | null;
  pageTotal: number;
  productList: IProduct[];
  goodProList: IGoods[];
}

const initalState: IState = {
  goodsList: [],
  goodsById: null,
  pageTotal: 0,
  productList: [],
  goodProList: []
};

export interface ITagList {
  id: string;
  name: string;
}

export const goods = createModel({
  state: initalState,
  reducers: {
    setGoodsList: (state: IState, payload): IState => {
      return { ...state, goodsList: payload };
    },
    goodsById: (state: IState, payload): IState => {
      return { ...state, goodsById: payload };
    },
    setGoodProList: (state: IState, payload): IState => {
      return { ...state, goodProList: payload };
    }
  },
  effects: dispatch => ({
    async getGoodsList(payload: any, rootState: IRootState) {
      const res = await getGoodsList(payload);
      if (res.status !== 200) {
        return;
      }
      initalState.pageTotal = res.data.totalElements;
      await dispatch.goods.setGoodsList(res.data.content);
    },
    async getGoodsById(payload: any, rootState: IRootState) {
      const res = await getGoodsById(payload);
      if (res.status !== 200) {
        return;
      }
      await dispatch.goods.goodsById(res.data);
    },
    async saveGoods(payload: any, rootState: IRootState) {
      const res = await saveGoods(payload);
      if (res.status !== 200) {
        message.warning(res.message);
        return;
      }
      message.success("保存成功!");
    },
    async editGoods(payload: any, rootState: IRootState) {
      const res = await editGoods(payload);
      if (res.status !== 200) {
        message.warning(res.message);
        return;
      }
      message.success("保存成功!");
      dispatch.goods.getGoodsList({
        page: 0,
        name: ""
      });
    },
    async uploadGoods(payload: any, rootState: IRootState) {
      const res = await uploadGoods(payload);
      if (res.status !== 200) {
        return;
      }
      message.success("保存成功!");
    },
    async deleteGoods(id: string, rootState: IRootState) {
      const res = await deleteGoods(id);
      if (res.status !== 200) {
        return;
      }
      message.success("删除成功!");
      dispatch.goods.getGoodsList({
        page: 0,
        name: ""
      });
    },
    async findByProId(id: string, rootState: IRootState) {
      const res = await findByProId(id);
      if (res.status !== 200) {
        return;
      }
      await dispatch.goods.setGoodProList(res.data);
    }
  })
});
